package com.f5.management.controller;

import com.f5.management.entity.Parking;
import com.f5.management.entity.ParkingManagement;
import com.f5.management.service.ParkingManagementService;
import com.f5.management.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("parking_management")
public class ParkingManagementController {
    @Autowired
    ParkingManagementService pmService;

    @GetMapping("findAll")
    public Map<String, Object> get() {
        HashMap<String, Object> map = new HashMap<>();
        Long count = pmService.count();

        List<ParkingManagement> list = pmService.list();

        map.put("count", count);
        map.put("list", list);
        return map;
    }

    @GetMapping("findAllParking")
    public List<Parking> getParking(String cname) {
        return pmService.freeParkingInCommunity(cname);
    }

    @PostMapping("update")
    public Result update(@RequestBody ParkingManagement management) {
        Result result = new Result();
        System.out.println(management);
        try {
            pmService.update(management);
            result.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("更新失败");
            result.setStatus(false);
        }
        return result;
    }

    @GetMapping("findByPage")
    public Map<String, Object> findByPage(Integer pageNow, Integer pageSize) {
        Map<String, Object> result= new HashMap<>();

        pageNow = (pageNow == null ? 1 : pageNow);
        pageSize = (pageSize == null ? 4 : pageSize);
        List<ParkingManagement> pms = pmService.findByPage(pageNow, pageSize);

        Long count = pmService.count();

        result.put("pms", pms);
        result.put("count", count);

        return result;
    }
}
